<?php
/*
Cogumelo v0.2 - Innoto S.L.
Copyright (C) 2010 Innoto Gestión para el Desarrollo Social S.L. <mapinfo@map-experience.com>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
USA.
*/

/*
 * Authcode Generator
 */


/*
 * Char table definition
 */
function chartable($code)
{
	switch($code)
	{
		case "36":
			$authchar = "A";
			break;
		case "35":
			$authchar = "B";
			break;
		case "34":
			$authchar = "C";
			break;
		case "33":
			$authchar = "D";
			break;
		case "32":
			$authchar = "E";
			break;
		case "31":
			$authchar = "F";
			break;
		case "30":
			$authchar = "G";
			break;
		case "29":
			$authchar = "H";
			break;
		case "28":
			$authchar = "I";
			break;
		case "27":
			$authchar = "J";
			break;
		case "26":
			$authchar = "K";
			break;
		case "25":
			$authchar = "L";
			break;
		case "24":
			$authchar = "M";
			break;
		case "23":
			$authchar = "N";
			break;
		case "22":
			$authchar = "O";
			break;
		case "21":
			$authchar = "P";
			break;
		case "20":
			$authchar = "Q";
			break;
		case "19":
			$authchar = "R";
			break;
		case "18":
			$authchar = "S";
			break;
		case "17":
			$authchar = "T";
			break;
		case "16":
			$authchar = "U";
			break;
		case "15":
			$authchar = "V";
			break;
		case "14":
			$authchar = "W";
			break;
		case "13":
			$authchar = "X";
			break;
		case "12":
			$authchar = "Y";
			break;
		case "11":
			$authchar = "Z";
			break;
		case "10":
			$authchar = "0";
			break;
		case "9":
			$authchar = "1";
			break;
		case "8":
			$authchar = "2";
			break;
		case "7":
			$authchar = "3";
			break;
		case "6":
			$authchar = "4";
			break;
		case "5":
			$authchar = "5";
			break;
		case "4":
			$authchar = "6";
			break;
		case "3":
			$authchar = "7";
			break;
		case "2":
			$authchar = "8";
			break;
		case "1":
			$authchar = "9";
			break;
	}
	return $authchar;
}


/*
 * Generate an AuthCode string of $n chars
 */
function getAuthcode($n)
{
	if($n)
	{
		$authstring="";
		for($i=0; $i<$n; ++$i)
		{
			mt_srand((double)microtime() * 1000000); //Get a nice seed
			$charcode = mt_rand(1,36);
  			$authstring .= chartable($charcode);
  		}
  	}
  	return $authstring;
  }
?>